/*
 * FullImageView.fx
 *
 * Created on 17-apr-2009, 10:24:21
 */

package fotoboek.gui;

import fotoboek.Main;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

/**
 * @author r.mohammadi
 */

// To display the full image
public class FullImageView extends CustomNode {

    public var x:Integer;
    public var y:Integer;
    public var width:Integer;
    public var height:Integer;
    public var image : Image;
    var sx:Number = 0;
    var ex:Number = 0;
     
    def imageView = ImageView {
        cache:false
        image: bind image
        smooth: false
        preserveRatio: false        
    }

    def bgRect = Rectangle {
        blocksMouse: true
        x: bind (x - 1)
        y: bind (y - 1)
        width: bind (width + 2)
        height: bind (height + 2)
        fill: Color.BLACK
        onMousePressed: function( e: MouseEvent ):Void {
                sx = e.dragX;
            }
        onMouseDragged: function( e: MouseEvent ):Void {
                ex = e.dragX;
            }
        onMouseReleased: function(e:MouseEvent) {
                Main.buttonBarShown = not Main.buttonBarShown;
                if(sx>ex) {
                    Main.showPreviousPhoto();
                }
                else if(sx<ex){
                    Main.showNextPhoto();
                }
            }
                     
    }
    override function create() : Node {
        Main.content.requestFocus();
        Main.buttonBarShown = false;
        Group { 
            content: [ bgRect, imageView ] 
            
        }
    }
    
    var imageHeight: Number = bind image.height on replace {
        if(image.height > 0.0) {
            preserveAspectRatio();
        }
    }
    def useEffects: Boolean =true;
    public var show = false on replace {
        imageView.translateX = x;
        imageView.translateY = y;
        imageView.fitWidth = width;
        imageView.fitHeight = height;
         if(useEffects) {
            fader();
        } else {
            visible = show;            
        }fader();
    }

    // Handles animation effect
    var timelineRate = 1.0;
    def timeline:Timeline = Timeline {
        rate: bind timelineRate with inverse
        keyFrames: [
        KeyFrame {
                time: 900ms
                canSkip: true
                action: function() {
                    if(timelineRate != -2) then preserveAspectRatio();
                }
            }
            KeyFrame {
                time: 1000ms
                values: [ opacity => 1.0 tween Interpolator.LINEAR ]
                canSkip: true
                action: function() {                  
                    if(timelineRate == -2) then Main.showScreen("full", "photo");
                }
            }
        ]
    };
    var timelineRunning = bind timeline.running on replace {
        if(not timelineRunning) { visible = show; }
    }

   public function preserveAspectRatio() {
        var scale = java.lang.Math.min(width/image.width, height/image.height);
        var finalW = image.width * scale;
        var finalH = image.height * scale;
        var finalX = x + ((width - finalW)/2.0);
        var finalY = y + ((height - finalH)/2.0);

        def parTimeline:Timeline = Timeline {
            keyFrames: [
                KeyFrame {
                    canSkip:true
                    time: 300ms
                    values: [
                        imageView.translateX => finalX tween Interpolator.LINEAR,
                        imageView.translateY => finalY tween Interpolator.LINEAR,
                        imageView.fitWidth => finalW tween Interpolator.LINEAR,
                        imageView.fitHeight => finalH tween Interpolator.LINEAR
                    ]
                }
            ]
        };
        if(useEffects) {
            parTimeline.play();
        } else {
            imageView.translateX = finalX;
            imageView.translateY = finalY;
            imageView.fitWidth = finalW;
            imageView.fitHeight = finalH;
        }
        Main.content.requestFocus();
    }
    function fader() {
        if(show) {
            timeline.time = 0s;
            timelineRate = 1.0;
            opacity = 0.0;
            visible = true;
        } else {
            timeline.time = 2s;
            timelineRate = -2.0;
            opacity = 1.0;
        }
        timeline.play();
    }
}

